package alipay

import (
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
)

// rsa公钥解码
func PublicKeyDecode(alipayRsaPublicKey string) *rsa.PublicKey {
	block, _ := pem.Decode([]byte(alipayRsaPublicKey))
	pub, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		panic(err)
	}
	publicKey, ok := pub.(*rsa.PublicKey)
	if !ok {
		panic("alipay alipayRsaPublicKey error ")
	}
	return publicKey
}

// rsa密钥解码
func PrivateKeyDecode(rsaPrivateKey string) *rsa.PrivateKey {
	block, _ := pem.Decode([]byte(rsaPrivateKey))
	privateKeyAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		panic(err)
	}
	privateKey, ok := privateKeyAny.(*rsa.PrivateKey)
	if !ok {
		panic("privateKey error")
	}
	return privateKey
}
